package laji;

import 课设.db;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;

class aaa extends JFrame {

    private JTextField username;
    private JButton ok,cancel;

    public aaa() {
        super();
        this.setSize(350,300);
        this.setTitle("新员工");
        this.setLocationRelativeTo(getOwner());//居中
        //设置组件布局
        Container cont = getContentPane();
        cont.setLayout(new GridLayout(3,2));
        //添加组件
        //------begin------

        //用户名
        cont.add(new JLabel("姓名"));
        username = new JTextField(10);
        cont.add(username);
        //
        cont.add(new JLabel("password"));
        JTextField notes = new JTextField(10);
        cont.add(notes);



        //按钮
        ok = new JButton("添加");
        cancel = new JButton("取消");
        cont.add(ok);
        cont.add(cancel);

        //注册监听器
        ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //ok事件处理：
                db dbcon = new db();   //插入操作
                try {
                    String sql = "insert into users values(?,?)";
                    PreparedStatement prestate = dbcon.PreparedStatement(sql);
                    prestate.setString(1, notes.getText());
                    prestate.setString(2, username.getText());

                    prestate.executeUpdate();
                    JOptionPane.showMessageDialog(null,"插入成功!");
                    dispose();
                    kaoshi psel = new kaoshi();
                    psel.setVisible(true);
                }
                catch(SQLException e) {
                    System.out.println(e.toString());
                }
            }
        });
    }
    public static void main(String[] agrs)
    {
        aaa w = new aaa();
        w.setVisible(true);
    }
}
